package subject_set.offer100;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/10/15 11:50
 **/
public class Offer32 {
    class Solution {
        public int[] levelOrder(TreeNode root) {
            if(root == null) return new int[0];
            List<Integer> list = new ArrayList<>();
            Deque<TreeNode> q = new LinkedList<>();
            q.offer(root);
            while(!q.isEmpty()){
                int size = q.size();
                for(int i = 0; i < size; ++i){
                    TreeNode cur = q.poll();
                    list.add(cur.val);
                    if(cur.left != null) q.offer(cur.left);
                    if(cur.right != null) q.offer(cur.right);
                }
            }
            int[] res = new int[list.size()];
            for(int i = 0; i < list.size(); ++i) {
                res[i] = list.get(i);
            }
            return res;
        }
    }
}